<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="项目名称">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item label="项目负责人">
              <a-input v-model="queryParam.director" placeholder="" />
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="项目来源">
                <j-dict-select-tag
                  v-model="queryParam.source"
                  placeholder="请输入项目来源！"
                  dictCode="research_project_category"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="经费资助">
                <j-dict-select-tag
                  v-model="queryParam.support"
                  placeholder="请输入项目来源！"
                  dictCode="research_project_level"
                />
              </a-form-item>
            </a-col>
          </template>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handlerApply" :disabled="!applyAble">项目申报</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'Project:add'">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('社团')" v-has="'Project:export'">导出</a-button> -->

      <!-- <a-button type="primary" icon="import" @click="visibleImport=true" v-has="'Project:edit'">导入</a-button> -->
      <!-- <XImport v-model="visibleImport" moduleName="researchProject" @ok="searchQuery" @cancel="visibleImport=false"></XImport> -->

      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Project:delete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" v-has="'Project:delete'" />批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      :scroll="{ x: 1000 }"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data="getData"
      @onSelect="onChange"
    >
      <span slot="action" slot-scope="text, record">
        <!-- <a @click="handleReview(record)">中期评审</a> -->
        <!-- <a style="margin-left:10px" @click="handleFinish(record)">结项</a> -->
        <a style="margin-left:10px" @click="handlePreview(record.id)">查看</a>
        <a @click="handleEdit(record)" v-has="'Project:edit'" style="margin-left:10px">编辑</a>
        <a-popconfirm
          style="margin-left:10px"
          title="确定删除?"
          v-has="'Project:delete'"
          @confirm="() => handleDelete(record.id)"
        >
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleApproval(record)" v-if="record.dealFlag">评审</a>
      </span>
    </s-table>

    <!-- 项目弹窗 -->
    <ProjectModal ref="modalForm" @ok="handleOk"></ProjectModal>

    <!-- 中期申报 -->
    <ProjectReviewModal ref="modalReviewForm"></ProjectReviewModal>

    <Approval
      v-model="shenpi"
      :bizId="mdl.id"
      businessUrl="/researchProject/add"
      :detailComponent="detailComponent"
      @ok="searchQuery"
    ></Approval>
  </a-card>
</template>

<script>
import moment from 'moment'
import Approval from '@/components/Approval/Approval'
import ProjectModal from './modules/ProjectModal'
import ProjectReviewModal from './modules/ProjectReviewModal'
import { getProjects, delProject } from '@/api/api'
import STable from '@/components/table/'
import XImport from '@/components/extends/XImport.vue'
import { httpAction, getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import find from 'lodash.find'
export default {
  name: 'projectManage',
  components: {
    STable,
    XImport,
    ProjectModal,
    ProjectReviewModal,
    JDictSelectTag,
    Approval
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      detailComponent: 'ProjectBuild',
      shenpi: false,
      disableMixinCreated: true,
      visibleImport: false,
      // 高级搜索 展开/关闭
      advanced: false,
      mdl: {},
      url: {
        list: '/researchProject/list',
        config: '/bizConfig/list',
        deleteBatch: '/researchProject/deleteBatch',
        exportXlsUrl: '/researchProject/exportXls'
      },
      // 查询参数
      queryParam: {},
      configList: [],
      columns: [
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'name',
          ellipsis: true,
          width: 250
        },
        {
          title: '项目来源',
          align: 'center',
          dataIndex: 'source_dictText',
          width: 150
        },
        {
          title: '经费资助',
          align: 'center',
          dataIndex: 'support_dictText',
          width: 150
        },
        {
          title: '起止年月',
          align: 'center',
          dataIndex: 'startTime',
          width: 150,
          customRender(text, record) {
            return (
              (record.startTime ? moment(record.startTime).format('YYYY-MM-DD') : '-') +
              ' 至 ' +
              (record.endTime ? moment(record.endTime).format('YYYY-MM-DD') : '-')
            )
          }
        },
        {
          title: '成果形式',
          align: 'center',
          dataIndex: 'resultType_dictText',
          width: 150
        },
        {
          title: '指导老师',
          align: 'center',
          dataIndex: 'instructor',
          width: 150
        },
        {
          title: '项目负责人',
          align: 'center',
          dataIndex: 'director',
          width: 150
        },
        {
          title: '项目成员',
          align: 'center',
          dataIndex: 'members',
          width: 250
        },
        {
          title: '申请金额',
          align: 'center',
          width: 150,
          dataIndex: 'fundAmount'
        },
        {
          title: '操作',
          align: 'center',
          width: 260,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      getData: parameter => {
        return getProjects(Object.assign({}, parameter, this.queryParam)).then(res => {
          if (res.success && res.result) {
            const obj = {
              data: res.result.records || [],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          } else {
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  computed: {
    applyAble() {
      let conf = find(this.configList, { type: 'TIME_RANGE' })
      if (conf) {
        let arr = conf.value.split('-')
        let now = +new Date()
        let start = +new Date(parseInt(arr[0]))
        let end = +new Date(parseInt(arr[1]))
        if (start < now && now < end) {
          return true
        }
      }
      return false
    }
  },
  mounted() {
    this.loadConfig()
  },
  methods: {
    loadConfig() {
      getAction(this.url.config, {
        module: 'research_project'
      }).then(res => {
        if (res.success && res.result) {
          this.configList = res.result.records
        } else {
          this.configList = []
        }
      })
    },
    handleAdd() {
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.add()
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    handlerApply() {
      this.$refs.modalForm.apply({
        director: {
          directorId: this.$store.getters.userInfo.id,
          name: this.$store.getters.userInfo.username
        }
      })
    },
    handleEdit(record) {
      this.$refs.modalForm.actStatus = record.actStatus
      this.$refs.modalForm.edit(record)
    },
    handleReview() {
      this.$refs.modalReviewForm.title = '中期申报'
      this.$refs.modalReviewForm.add({ pid: this.pid })
    },
    handlePreview(id) {
      // this.$refs.modalForm.title = '查看'
      // this.$refs.modalForm.footer = false
      this.$refs.modalForm.preview({ id: id })
    },
    handleApproval(record) {
      this.mdl = record
      this.shenpi = true
    },
    handleDelete(id) {
      delProject({ id }).then(res => {
        if (res.success) {
          this.$message.success('删除成功')
          this.searchQuery()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    batchDel: function() {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置api.leteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            that.loading = true
            deleteAction(that.url.deleteBatch, { ids: ids })
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.selectedRowKeys = []
                  that.selectionRows = []
                  that.searchQuery()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          }
        })
      }
    },
    searchReset() {
      this.queryParam = {}
      this.searchQuery()
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onChange() {}
  }
}
</script>

<style></style>
